package com.morehair.typehandler;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @Author: DLX
 * @Date: 2021/6/6 23:47
 * @Description:
 **/

@MappedTypes(List.class)
@MappedJdbcTypes(JdbcType.VARCHAR)
public class ListTypeHandler implements TypeHandler<List<?>> {

    ObjectMapper objectMapper = new ObjectMapper();
    @SneakyThrows
    @Override
    public void setParameter(PreparedStatement preparedStatement, int index, List<?> objects, JdbcType jdbcType) throws SQLException {
        String string = objectMapper.writeValueAsString(objects);
        preparedStatement.setString(index, string);

    }

    @Override
    public List<?> getResult(ResultSet resultSet, String s) throws SQLException {
        return null;
    }

    @Override
    public List<?> getResult(ResultSet resultSet, int i) throws SQLException {
        return null;
    }

    @Override
    public List<?> getResult(CallableStatement callableStatement, int i) throws SQLException {
        return null;
    }
}
